package com.lush.middleware.web;

import cn.hutool.core.util.StrUtil;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/")
public class QrController {

    /**
     * 创建二维码
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/qrcode")
    public void addQrcode(HttpServletRequest request, HttpServletResponse response) {
        String content=request.getParameter("code");
        String widthStr=request.getParameter("width");
        String heightStr=request.getParameter("height");
        int width= StrUtil.isBlank(widthStr)?150:Integer.valueOf(widthStr).intValue();
        int height= StrUtil.isBlank(heightStr)?150:Integer.valueOf(heightStr).intValue();

        QRCodeWriter writer = new QRCodeWriter();
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        BitMatrix bitMatrix = null;
        try {
            bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height);
            MatrixToImageWriter.writeToStream(bitMatrix, "jpeg", response.getOutputStream());
            response.getOutputStream().flush();
            response.getOutputStream().close();
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 创建二维码
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/barcode")
    public void barQrcode(HttpServletRequest request, HttpServletResponse response) {
        String content=request.getParameter("code");
        String widthStr=request.getParameter("width");
        String heightStr=request.getParameter("height");
        int width= StrUtil.isBlank(widthStr)?40:Integer.valueOf(widthStr).intValue();
        int height= StrUtil.isBlank(heightStr)?20:Integer.valueOf(heightStr).intValue();

        Code128Writer  writer = new Code128Writer ();
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        BitMatrix bitMatrix = null;
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");     // 指定编码格式
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);  // 指定纠错级别(L--7%,M--15%,Q--25%,H--30%)
        hints.put(EncodeHintType.MARGIN,0);
        try {
            // 编码内容, 编码类型, 宽度, 高度, 设置参数
            bitMatrix = writer.encode(content, BarcodeFormat.CODE_128, width, height);
            MatrixToImageWriter.writeToStream(bitMatrix, "jpeg", response.getOutputStream());
            response.getOutputStream().flush();
            response.getOutputStream().close();
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
